const app = getApp()
const now = new Date()
const util = require('../../utils/util')
Page({
  data: {
    title: "",
    dese: "",
    show: "",
    num: "",
    price: "",
    local: "",
    others: "",
    dater: util.formatYMD(now),
    time: util.formatHM(now),

    index: 0,
    typearray: ['火锅', '鸡公煲', '海底捞', '铁板烧', '干锅', '快餐', '中餐', '烤肉', '自助餐', '其他'],
    imagearry: [
      'https://i.loli.net/2020/06/05/4miCpNPrn5W7MLe.gif',
      'https://i.loli.net/2020/06/05/Tayz2efFrD5RMLB.gif',
      'https://i.loli.net/2020/06/05/GNMq8XDIYEtTu4c.gif',
      'https://i.loli.net/2020/06/05/gFvtf6Bs1aD83jJ.gif',
      'https://i.loli.net/2020/06/05/z2hbEuGTSQN79mC.gif',
      'https://i.loli.net/2020/06/05/hpTrqF7vinwmyeA.gif',
      'https://sm.ms/delete/PwJazq1Dd8oTr4BtQ59xheObGI',
      'https://i.loli.net/2020/06/05/MXp9RVLrkUsc136.gif',
      'https://i.loli.net/2020/06/05/CQ4u1ZJ39VTUW7w.gif',
      'https://i.loli.net/2020/06/05/SAmN8KWkFEosj4h.gif',
    ]
  },

  bindinputTitle: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  bindinputDese: function (e) {
    this.setData({
      dese: e.detail.value
    })
  },
  bindchangeType: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindinputNum: function (e) {
    this.setData({
      num: e.detail.value
    })
  },
  bindinputLoacl: function (e) {
    this.setData({
      local: e.detail.value
    })
  },
  bindchangeDate: function (e) {
    this.setData({
      dater: e.detail.value
    })
  },
  bindchangeTime: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  bindinputOthers: function (e) {
    this.setData({
      others: e.detail.value
    })
  },

  sumbit: function (e) {
    const db = wx.cloud.database()
    db.collection('canteen').add({
      data: {
        title: this.data.title,
        dese: this.data.dese,
        show: this.data.typearray[this.data.index],
        num: this.data.num,
        price: new Date(this.data.dater+' '+this.data.time),
        local: this.data.local,
        others: this.data.others,
        image: this.data.imagearry[this.data.index]
      },
      success: res => {
        console.log(res)
        this.setData({
        })
        wx.showToast({
          title: '信息发布成功',
          icon: 'success',
          duration: 2000
        })
        console.log('新增数据成功，记录_id:', res._id)
        wx.redirectTo({
          url: '../canteen/canteen',
        })
      },
      fail: err => {
        wx.showToast({
          title: '信息发布失败',
          icon: 'none'
        })
        console.error('信息发布失败', err)
      }
    })
  },
})